package com.iris.live.services.report.sources;

import com.iris.live.services.common.DateFormatUtils;
import com.iris.live.services.common.GetConfigureInfoUtils;
import com.iris.live.services.common.StringUtils;
import com.iris.live.services.data.repositories.DataCheckRepository;
import com.iris.live.services.report.Cell;
import com.iris.live.services.report.IExcelReportSource;
import com.iris.live.services.report.Row;
import com.iris.live.services.report.Sheet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by lusong on 2017/5/24.
 */
@Component("ExportDataCheckScContrastReportSource")
public class ExportDataCheckScContrastReportSource implements IExcelReportSource {

    private Logger logger = LoggerFactory.getLogger(ExportDataCheckScContrastReportSource.class);

    private String manualData = GetConfigureInfoUtils.getConfigureInfo("file_group") + "_经销商_数据核验_销售顾问_手工台账数据";
    private String systemData = GetConfigureInfoUtils.getConfigureInfo("file_group") + "_经销商_数据核验_销售顾问_LiVE系统数据";

    @Autowired
    private DataCheckRepository dataCheckRepository;

    @Override
    public Sheet[] getSheets(Map<String, Object> context) {
        Sheet[] sheets = new Sheet[2];
        sheets[0] = new Sheet(manualData,manualData,true);
        sheets[1] = new Sheet(systemData,systemData,true);
         return sheets;
    }

    @Override
    public List<Row> getSheetRows(Map<String, Object> context, Sheet sheet) throws ParseException {
        List<Row> values = new ArrayList<>();

        String permit = context.get("permit") == null ? null : context.get("permit").toString();
        String startDate = context.get("startDate") == null ? null : context.get("startDate").toString();
        String endDate = context.get("endDate") == null ? null : context.get("endDate").toString();
        String nowDate = null;
        if (StringUtils.isNullOrBlank(startDate) && StringUtils.isNullOrBlank(endDate)) {
            nowDate = DateFormatUtils.getFormatDateToString(new java.util.Date(), "yyyyMMdd");
        }
        List list = dataCheckRepository.getScContrast(permit, nowDate, startDate, endDate);

        values.add(new Row());
        values.add(new Row());
        values.add(new Row());
        values.add(new Row());
        values.add(new Row());
        values.add(new Row());

        if (list != null && !list.isEmpty()) {
            for (Object o : list) {
                Map<String, Object> map = (Map) o;
                if (manualData.equals(sheet.getName())) {
                    values.add(new Row(getCells(map)));
                } else if (systemData.equals(sheet.getName())) {
                    values.add(new Row(getOtherCells(map)));
                }
            }
        }
        return values;
    }

    private Cell[] getCells (Map<String, Object> map) {
        Cell[] cells = new Cell[34];
        int index = 0;
        cells[index++] = new Cell(map.get("customTel") == null ? "" : map.get("customTel").toString(), index, !"02".equals(map.get("compareResult") == null ? null : map.get("compareResult").toString()));
        cells[index++] = new Cell(map.get("sc") == null ? "" : map.get("sc").toString(), index, !"02".equals(map.get("compareResult") == null ? null : map.get("compareResult").toString()));
        cells[index++] = new Cell(map.get("vin") == null ? "" : map.get("vin").toString(), index, !"02".equals(map.get("compareResult") == null ? null : map.get("compareResult").toString()));
        cells[index++] = new Cell(map.get("intentCar") == null ? "" : map.get("intentCar").toString(), index, !"02".equals(map.get("compareResult") == null ? null : map.get("compareResult").toString()));
        cells[index++] = new Cell(map.get("salesStatus") == null ? "" : map.get("salesStatus").toString(), index, !"02".equals(map.get("compareResult") == null ? null : map.get("compareResult").toString()));
        cells[index++] = new Cell(map.get("ioDt") == null ? "" : map.get("ioDt").toString(), index, !"1".equals(map.get("dIoDt") == null ? null : map.get("dIoDt").toString()));
        cells[index++] = new Cell(map.get("orderCusName") == null ? "" : map.get("orderCusName").toString(), index, !"1".equals(map.get("dOrderCusName") == null ? null : map.get("dOrderCusName").toString()));
        cells[index++] = new Cell(map.get("ioAmount") == null ? "" : map.get("ioAmount").toString(), index, !"1".equals(map.get("dIoAmount") == null ? null : map.get("dIoAmount").toString()));
        cells[index++] = new Cell(map.get("contractSignedDt") == null ? "" : map.get("contractSignedDt").toString(), index, !"1".equals(map.get("dContractSignedDt") == null ? null : map.get("dContractSignedDt").toString()));
        cells[index++] = new Cell(map.get("invoiceDt") == null ? "" : map.get("invoiceDt").toString(), index, !"1".equals(map.get("dInvoiceDt") == null ? null : map.get("dInvoiceDt").toString()));
        cells[index++] = new Cell(map.get("invoiceUname") == null ? "" : map.get("invoiceUname").toString(), index, !"1".equals(map.get("dInvoiceUname") == null ? null : map.get("dInvoiceUname").toString()));
        cells[index++] = new Cell(map.get("deliverDt") == null ? "" : map.get("deliverDt").toString(), index, !"1".equals(map.get("dDeliverDt") == null ? null : map.get("dDeliverDt").toString()));
        cells[index++] = new Cell(map.get("priceTi") == null ? "" : map.get("priceTi").toString(), index, !"1".equals(map.get("dPriceTi") == null ? null : map.get("dPriceTi").toString()));
        cells[index++] = new Cell(map.get("freebyIn") == null ? "" : map.get("freebyIn").toString(), index, !"1".equals(map.get("dFreebyIn") == null ? null : map.get("dFreebyIn").toString()));
        cells[index++] = new Cell(map.get("freebySubInt") == null ? "" : map.get("freebySubInt").toString(), index, !"1".equals(map.get("dFreebySubInt") == null ? null : map.get("dFreebySubInt").toString()));
        cells[index++] = new Cell(map.get("freebyA") == null ? "" : map.get("freebyA").toString(), index, !"1".equals(map.get("dFreebyA") == null ? null : map.get("dFreebyA").toString()));
        cells[index++] = new Cell(map.get("freebyB") == null ? "" : map.get("freebyB").toString(), index, !"1".equals(map.get("dFreebyB") == null ? null : map.get("dFreebyB").toString()));
        cells[index++] = new Cell(map.get("freebySi") == null ? "" : map.get("freebySi").toString(), index, !"1".equals(map.get("dFreebySi") == null ? null : map.get("dFreebySi").toString()));
        cells[index++] = new Cell(map.get("freebyBl") == null ? "" : map.get("freebyBl").toString(), index, !"1".equals(map.get("dFreebyBl") == null ? null : map.get("dFreebyBl").toString()));
        cells[index++] = new Cell(map.get("freebySh") == null ? "" : map.get("freebySh").toString(), index, !"1".equals(map.get("dFreebySh") == null ? null : map.get("dFreebySh").toString()));
        cells[index++] = new Cell(map.get("freebyOi") == null ? "" : map.get("freebyOi").toString(), index, !"1".equals(map.get("dFreebyOi") == null ? null : map.get("dFreebyOi").toString()));
        cells[index++] = new Cell(map.get("freebyBo") == null ? "" : map.get("freebyBo").toString(), index, !"1".equals(map.get("dFreebyBo") == null ? null : map.get("dFreebyBo").toString()));
        cells[index++] = new Cell(map.get("freebyIsIn") == null ? "" : map.get("freebyIsIn").toString(), index, !"1".equals(map.get("dFreebyIsIn") == null ? null : map.get("dFreebyIsIn").toString()));
        cells[index++] = new Cell(map.get("freebyIsSssc") == null ? "" : map.get("freebyIsSssc").toString(), index, !"1".equals(map.get("dFreebyIsSssc") == null ? null : map.get("dFreebyIsSssc").toString()));
        cells[index++] = new Cell(map.get("freebyIsSd") == null ? "" : map.get("freebyIsSd").toString(), index, !"1".equals(map.get("dFreebyIsSd") == null ? null : map.get("dFreebyIsSd").toString()));
        cells[index++] = new Cell(map.get("freebyIsOil") == null ? "" : map.get("freebyIsOil").toString(), index, !"1".equals(map.get("dFreebyIsOil") == null ? null : map.get("dFreebyIsOil").toString()));
        cells[index++] = new Cell(map.get("freebyIsPre") == null ? "" : map.get("freebyIsPre").toString(), index, !"1".equals(map.get("dFreebyIsPre") == null ? null : map.get("dFreebyIsPre").toString()));
        cells[index++] = new Cell(map.get("freebyIsTi") == null ? "" : map.get("freebyIsTi").toString(), index, !"1".equals(map.get("dFreebyIsTi") == null ? null : map.get("dFreebyIsTi").toString()));
        cells[index++] = new Cell(map.get("purTaxSi") == null ? "" : map.get("purTaxSi").toString(), index, !"1".equals(map.get("dPurTaxSi") == null ? null : map.get("dPurTaxSi").toString()));
        cells[index++] = new Cell(map.get("boPriceTi") == null ? "" : map.get("boPriceTi").toString(), index, !"1".equals(map.get("dBoPriceTi") == null ? null : map.get("dBoPriceTi").toString()));
        cells[index++] = new Cell(map.get("exInPriceTi") == null ? "" : map.get("exInPriceTi").toString(), index, !"1".equals(map.get("dExInPriceTi") == null ? null : map.get("dExInPriceTi").toString()));
        cells[index++] = new Cell(map.get("licFee") == null ? "" : map.get("licFee").toString(), index, !"1".equals(map.get("dLicFee") == null ? null : map.get("dLicFee").toString()));
        cells[index++] = new Cell(map.get("othFee") == null ? "" : map.get("othFee").toString(), index, !"1".equals(map.get("dOthFee") == null ? null : map.get("dOthFee").toString()));
        cells[index++] = new Cell(map.get("finFee") == null ? "" : map.get("finFee").toString(), index, !"1".equals(map.get("dFinFee") == null ? null : map.get("dFinFee").toString()));
        return cells;
    }


    private Cell[] getOtherCells (Map<String, Object> map) {
        Cell[] cells = new Cell[34];
        int index = 0;
        cells[index++] = new Cell(map.get("customTel") == null ? "" : map.get("customTel").toString(), index, !"02".equals(map.get("compareResult") == null ? null : map.get("compareResult").toString()));
        cells[index++] = new Cell(map.get("sc") == null ? "" : map.get("sc").toString(), index, !"02".equals(map.get("compareResult") == null ? null : map.get("compareResult").toString()));
        cells[index++] = new Cell(map.get("vin") == null ? "" : map.get("vin").toString(), index, !"02".equals(map.get("compareResult") == null ? null : map.get("compareResult").toString()));
        cells[index++] = new Cell(map.get("intentCar") == null ? "" : map.get("intentCar").toString(), index, !"02".equals(map.get("compareResult") == null ? null : map.get("compareResult").toString()));
        cells[index++] = new Cell(map.get("salesStatus") == null ? "" : map.get("salesStatus").toString(), index, !"02".equals(map.get("compareResult") == null ? null : map.get("compareResult").toString()));
        cells[index++] = new Cell(map.get("cioDt") == null ? "" : map.get("cioDt").toString(), index, !"1".equals(map.get("dIoDt") == null ? null : map.get("dIoDt").toString()));
        cells[index++] = new Cell(map.get("corderCusName") == null ? "" : map.get("corderCusName").toString(), index, !"1".equals(map.get("dOrderCusName") == null ? null : map.get("dOrderCusName").toString()));
        cells[index++] = new Cell(map.get("cioAmount") == null ? "" : map.get("cioAmount").toString(), index, !"1".equals(map.get("dIoAmount") == null ? null : map.get("dIoAmount").toString()));
        cells[index++] = new Cell(map.get("ccontractSignedDt") == null ? "" : map.get("ccontractSignedDt").toString(), index, !"1".equals(map.get("dContractSignedDt") == null ? null : map.get("dContractSignedDt").toString()));
        cells[index++] = new Cell(map.get("cinvoiceDt") == null ? "" : map.get("cinvoiceDt").toString(), index, !"1".equals(map.get("dInvoiceDt") == null ? null : map.get("dInvoiceDt").toString()));
        cells[index++] = new Cell(map.get("cinvoiceUname") == null ? "" : map.get("cinvoiceUname").toString(), index, !"1".equals(map.get("dInvoiceUname") == null ? null : map.get("dInvoiceUname").toString()));
        cells[index++] = new Cell(map.get("cdeliverDt") == null ? "" : map.get("cdeliverDt").toString(), index, !"1".equals(map.get("dDeliverDt") == null ? null : map.get("dDeliverDt").toString()));
        cells[index++] = new Cell(map.get("cpriceTi") == null ? "" : map.get("cpriceTi").toString(), index, !"1".equals(map.get("dPriceTi") == null ? null : map.get("dPriceTi").toString()));
        cells[index++] = new Cell(map.get("cfreebyIn") == null ? "" : map.get("cfreebyIn").toString(), index, !"1".equals(map.get("dFreebyIn") == null ? null : map.get("dFreebyIn").toString()));
        cells[index++] = new Cell(map.get("cfreebySubInt") == null ? "" : map.get("cfreebySubInt").toString(), index, !"1".equals(map.get("dFreebySubInt") == null ? null : map.get("dFreebySubInt").toString()));
        cells[index++] = new Cell(map.get("cfreebyA") == null ? "" : map.get("cfreebyA").toString(), index, !"1".equals(map.get("dFreebyA") == null ? null : map.get("dFreebyA").toString()));
        cells[index++] = new Cell(map.get("cfreebyB") == null ? "" : map.get("cfreebyB").toString(), index, !"1".equals(map.get("dFreebyB") == null ? null : map.get("dFreebyB").toString()));
        cells[index++] = new Cell(map.get("cfreebySi") == null ? "" : map.get("cfreebySi").toString(), index, !"1".equals(map.get("dFreebySi") == null ? null : map.get("dFreebySi").toString()));
        cells[index++] = new Cell(map.get("cfreebyBl") == null ? "" : map.get("cfreebyBl").toString(), index, !"1".equals(map.get("dFreebyBl") == null ? null : map.get("dFreebyBl").toString()));
        cells[index++] = new Cell(map.get("cfreebySh") == null ? "" : map.get("cfreebySh").toString(), index, !"1".equals(map.get("dFreebySh") == null ? null : map.get("dFreebySh").toString()));
        cells[index++] = new Cell(map.get("cfreebyOi") == null ? "" : map.get("cfreebyOi").toString(), index, !"1".equals(map.get("dFreebyOi") == null ? null : map.get("dFreebyOi").toString()));
        cells[index++] = new Cell(map.get("cfreebyBo") == null ? "" : map.get("cfreebyBo").toString(), index, !"1".equals(map.get("dFreebyBo") == null ? null : map.get("dFreebyBo").toString()));
        cells[index++] = new Cell(map.get("cfreebyIsIn") == null ? "" : map.get("cfreebyIsIn").toString(), index, !"1".equals(map.get("dFreebyIsIn") == null ? null : map.get("dFreebyIsIn").toString()));
        cells[index++] = new Cell(map.get("cfreebyIsSssc") == null ? "" : map.get("cfreebyIsSssc").toString(), index, !"1".equals(map.get("dFreebyIsSssc") == null ? null : map.get("dFreebyIsSssc").toString()));
        cells[index++] = new Cell(map.get("cfreebyIsSd") == null ? "" : map.get("cfreebyIsSd").toString(), index, !"1".equals(map.get("dFreebyIsSd") == null ? null : map.get("dFreebyIsSd").toString()));
        cells[index++] = new Cell(map.get("cfreebyIsOil") == null ? "" : map.get("cfreebyIsOil").toString(), index, !"1".equals(map.get("dFreebyIsOil") == null ? null : map.get("dFreebyIsOil").toString()));
        cells[index++] = new Cell(map.get("cfreebyIsPre") == null ? "" : map.get("cfreebyIsPre").toString(), index, !"1".equals(map.get("dFreebyIsPre") == null ? null : map.get("dFreebyIsPre").toString()));
        cells[index++] = new Cell(map.get("cfreebyIsTi") == null ? "" : map.get("cfreebyIsTi").toString(), index, !"1".equals(map.get("dFreebyIsTi") == null ? null : map.get("dFreebyIsTi").toString()));
        cells[index++] = new Cell(map.get("cpurTaxSi") == null ? "" : map.get("cpurTaxSi").toString(), index, !"1".equals(map.get("dPurTaxSi") == null ? null : map.get("dPurTaxSi").toString()));
        cells[index++] = new Cell(map.get("cboPriceTi") == null ? "" : map.get("cboPriceTi").toString(), index, !"1".equals(map.get("dBoPriceTi") == null ? null : map.get("dBoPriceTi").toString()));
        cells[index++] = new Cell(map.get("cexInPriceTi") == null ? "" : map.get("cexInPriceTi").toString(), index, !"1".equals(map.get("dExInPriceTi") == null ? null : map.get("dExInPriceTi").toString()));
        cells[index++] = new Cell(map.get("clicFee") == null ? "" : map.get("clicFee").toString(), index, !"1".equals(map.get("dLicFee") == null ? null : map.get("dLicFee").toString()));
        cells[index++] = new Cell(map.get("cothFee") == null ? "" : map.get("cothFee").toString(), index, !"1".equals(map.get("dOthFee") == null ? null : map.get("dOthFee").toString()));
        cells[index++] = new Cell(map.get("cfinFee") == null ? "" : map.get("cfinFee").toString(), index, !"1".equals(map.get("dFinFee") == null ? null : map.get("dFinFee").toString()));
        return cells;
    }
}
